/*
 * LinearAlloc.c
 *
 *  Created on: 2011-7-9
 *      Author: gexiao
 */
/*
 * Linear memory allocation, tied to class loaders.
 */
#include "Dalvik.h"
#include "alloc/cutils/ashmem.h"

#include <limits.h>

/*
 * Overview
 *
 * This is intended to be s simple, fast allocator for "write-once" storage.
 * The expectation is that this will hold small allocations that don't change,
 * such as parts of classes(vtables, fields methods,interfaces). Because
 * the lifetime of these items is tied to classes, which in turn are tied
 * to class loaders, we associate the storage with a ClassLoader object.
 *
 * [ We don't yet support class unloading, and our ClassLoader implementation
 * is in flux, so for now we just have a single global region and the
 * "classLoader" argument is ignored. ]
 *
 * By storing the data here, rather than on the system heap, we reduce heap
 * clutter, speed class loading, reduce the memory footprint (reduced heap
 * structure overhead), and most importantly we increase the number of pages
 * that remain shared between processes launched in "Zygote mode".
 *
 * The 4 bytes preceding each block contain the block length. This allows us
 * to support "free" and "realloc" calls in a limited way. We don't free storage
 * once it has been allocated, but in some circumstances it could be
 * useful to erase storage to garbage values after a "free" or "realloc".
 * ...
 */

//alignment for allocations; must be power of 2, and currently >= hdr_xtra
#define BLOCK_ALIGN 8

//default length of memory segment (worst case is probably "dexopt")
#define DEFAULT_MAX_LENGTH (4*1024*1024)

//leave enough space for a length word
#define HEADER_EXTRA 4

#define PAGESIZE 4096

/*
 * Create a new linear allocation block.
 */
LinearAllocHdr* dvmLinearAllocCreate(Object* classLoader) {
	LinearAllocHdr* pHdr;

	pHdr = (LinearAllocHdr*) malloc(sizeof(*pHdr));

	/*
	 * "curOffset" points to the location of the next pre-block header,
	 *
	 */
	//some code here
}

/*
 *
 */
void* dvmLinearAlloc(Object* classLoader, size_t size) {

}

/*
 * "Reallocate" a piece of memory.
 *
 * If the new size is <= the old size, we return the original pointer
 * without doing anything.
 *
 * If the new size is > the old size, we allocate new storage, copy the
 * old stuff over, and mark the new stuff as free.
 */
void* dvmLinearRealloc(Object* classLoader, void* mem, size_t newSize) {

}

/*
 * Mark an allocation as free.
 */
void dvmLinearFree(Object* classLoader, void* mem) {

}
